<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('查询脚本') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('编码')" prop="code">
        <el-input
          v-model="queryParams.code"
          :placeholder="$t('编码')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('名称')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="$t('名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:search:edit']"
          type="primary"
          size="mini"
          icon="el-icon-plus"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="searchSqlBaseList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('编码')" align="center" prop="code" />
      <el-table-column :label="$t('名称')" align="center" prop="name">
        <template slot-scope="scope">
          <span>{{ $t(scope.row.name) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:search:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['system:search:edit']"
            size="small"
            type="text"
            icon="el-icon-success"
            @click="arrangeHandle(scope.row)"
          >{{ $t('部署') }}</el-button>
          <el-button
            v-hasPermi="['system:search:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改高级查询sql对话框 -->
    <el-dialog :title="$t('查询脚本')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <searchSqlBaseForm ref="searchSqlBaseForm" />
    </el-dialog>

    <el-dialog :title="$t('部署')" :visible.sync="deploymentOpen" :close-on-click-modal="false" :width="'30%'" :lock-scroll="false" append-to-body>
      <div v-loading="loading">
        <el-form ref="form" :model="deploymentForm" :rules="deploymentFormRules" label-width="120px" class="dataForm">
          <el-row>
            <el-col :span="24">
              <el-form-item :label="$t('更新范围')" prop="fieldUpdateScope">
                <el-select v-model="deploymentForm.fieldUpdateScope" multiple style="width: 100px;">
                  <el-option :label="$t('是否展示')" value="ZS" />
                  <el-option :label="$t('是否固定')" value="GD" />
                  <el-option :label="$t('宽度')" value="KD" />
                  <el-option :label="$t('排序')" value="PX" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align: right; margin-top: 20px;">
          <el-button @click="deploymentOpen = false">{{ $t('取消') }}</el-button>
          <el-button type="primary" @click="submitDeployment">{{ $t('保存') }}</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSearchSqlBase,
  delSearchSqlBase,
  exportSearchSqlBase,
  submitDeployment
} from '@/api/system/search/searchSqlBase'
import searchSqlBaseForm from './searchSqlBaseForm.vue'
export default {
    components: {
        searchSqlBaseForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 高级查询sql表格数据
          searchSqlBaseList: [],
          // 是否显示弹出层
          formOpen: false,
          deploymentOpen: false,
          // 查询参数
          queryParams: {
              code: undefined,
              searchSql: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          deploymentForm: {
            viewId: undefined,
            code: undefined,
            fieldUpdateScope: []
          },
          // 表单校验
          rules: {
          },
          deploymentFormRules: {
            viewId: [{ required: true, message: this.$t('必填信息'),	trigger: 'change' }],
            code: [{ required: true, message: this.$t('必填信息'),	trigger: 'change' }],
            fieldUpdateScope: [{ required: true, message: this.$t('必填信息'),	trigger: 'change' }]
          }
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询高级查询sql列表 */
    getList() {
        this.loading = true
        listSearchSqlBase(this.queryParams).then(response => {
            this.searchSqlBaseList = response.rows
            this.total = response.total
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            code: undefined,
            searchSql: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.searchSqlBaseForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.searchSqlBaseForm.init(row)
        })
    },
    /** 部署按钮操作 */
    arrangeHandle(row) {
      this.deploymentForm = {
        viewId: row.id,
        code: row.code,
        fieldUpdateScope: ['ZS', 'GD', 'KD', 'PX']
      }
      this.deploymentOpen = true
    },
    /** 执行部署 */
    submitDeployment: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          submitDeployment(this.deploymentForm).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('部署成功'))
              this.deploymentOpen = false
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delSearchSqlBase(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportSearchSqlBase(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    }
  }
}
</script>
